//node 2_有规律的合并两个数组.js

// push()-入栈尾 
//  pop()-出栈尾  
// unshift()-入栈顶 
//   shift()-出栈顶

// new RegExp() 用来支持正则表达式，接收一个模式（即正则表达式，可以是字符）
// exec() 教收查找目标字符串参数，返回匹配的数组（数组项参数：index-匹配的起始位置,input-要查找的字符串）
// test() 接收字符串参数，如果接收的文本与模式匹配，则返回true,否则返回false


// 数组arr1:[A1,A2,A3,B1,B2,B3,C1,C2,C3,D1,D2]
// 数组arr2:[A,B,C,D]
// 目标结果数组:[A1,A2,A3,A,B1,B2,B3,B,C1,C2,C3,C,D1,D2,D]

var arr1 = ["A1","A2","A3","B1","B2","B3","C1","C2","C3","D1","D2"]
var arr2 = ["A","B","C","D"]

//方法一：
function operateArr(arr1,arr2) {
    let newArr1 = arr1
    let arr1Index = 0
    for(let item of arr2){
        const reg = new RegExp(item)
        while(arr1Index < newArr1.length){
            arr1Index++
            if(!reg.test(arr1[arr1Index])){
                newArr1.splice(arr1Index,0,item)
                break
            }
        }
    }
    return newArr1
    
}
//方法二:
function  operateArr2(arr1,arr2) {
    let newArr1 = []
    let arr2Index = 0
    for(item of arr1){
        if(item[0] != arr2[arr2Index][0]) newArr1.push(arr2[arr2Index++])
        newArr1.push(item)
    }
    newArr1.push(arr2[arr2.length-1])
    return newArr1
}

console.log(operateArr(arr1,arr2))
console.log(operateArr2(arr1,arr2))